package com.shengdong.water.code.repository

import com.shengdong.water.base.BaseRepository
import com.shengdong.water.base.ResultState
import com.shengdong.water.code.api.ApiService
import com.shengdong.water.code.bean.GovernBean
import com.shengdong.water.code.bean.MapDataBean
import com.shengdong.water.code.bean.MapOutletBean
import com.shengdong.water.code.bean.NotifyMsg
import com.shengdong.water.code.bean.Outlet
import com.shengdong.water.code.bean.OutletCategoryBean
import com.shengdong.water.code.bean.PageData
import com.shengdong.water.code.bean.RealTimeBean
import com.shengdong.water.code.bean.SignInBean
import com.shengdong.water.code.bean.TreeNode
import com.shengdong.water.code.bean.UploadBean
import com.shengdong.water.code.bean.UserToken
import com.shengdong.water.code.utils.JsonUtil
import kotlinx.coroutines.flow.Flow
import okhttp3.MultipartBody
import java.util.Objects
import javax.inject.Inject

class ApiRepository @Inject constructor(
    private val apiService: ApiService
) : BaseRepository() {

    fun login(username: String, password: String): Flow<ResultState<UserToken>> {
        return safeApiCall { apiService.userToken("password",username, password) }
    }

    fun getNotify(current: Int, size: Int): Flow<ResultState<PageData<NotifyMsg>>> {
        return safeApiCall { apiService.getNotifyMsg(current, size) }
    }


    fun getAllOutlets(): Flow<ResultState<MutableList<Outlet>>> {
        return safeApiCall { apiService.getAllOutlet() }
    }

    fun getOutletList(name: String,current: Int, size: Int): Flow<ResultState<PageData<Outlet>>> {
        return safeApiCall { apiService.getOutletList(name,current, size) }
    }

    fun submitSign(map: MutableMap<String, Any>): Flow<ResultState<Any>> {
        val jsonRequestBody = JsonUtil.mapToJsonRequestBody(map)
        return safeApiCall { apiService.submitSign(jsonRequestBody) }
    }

    fun getSignList(sign: MutableMap<String, Any>, current: Int, size: Int): Flow<ResultState<PageData<SignInBean>>> {
        return safeApiCall { apiService.getSignList(sign, current, size) }
    }

    fun getSignDetail(id: String): Flow<ResultState<SignInBean>> {
        return safeApiCall { apiService.getSignDetail(id) }
    }


    fun getOutletCategory(): Flow<ResultState<MutableList<OutletCategoryBean>>> {
        return safeApiCall { apiService.getDictionary() }
    }

    fun upLoadFile (file : MultipartBody.Part) : Flow<ResultState<UploadBean>>{
        return safeApiCall { apiService.uploadFile(file) }
    }

    fun getGovernList(current: Int, size: Int): Flow<ResultState<PageData<GovernBean>>> {
        return safeApiCall { apiService.getGovernList(current, size) }
    }


    fun  getRealTimeList(queryMap: Map<String, Any>): Flow<ResultState<PageData<RealTimeBean>>> {
        return safeApiCall { apiService.getRealTimeList(queryMap) }
    }

    fun  getRealTimeDetail(id: String): Flow<ResultState<RealTimeBean>> {
        return safeApiCall { apiService.getRealTimeDetail(id) }
    }

    fun getAdministrativeArea(level: Int, keyword: String): Flow<ResultState<MapDataBean>> {
        return safeApiCall { apiService.getAdministrativeArea(level,keyword) }
    }

    fun getOutletInfoOnMap(): Flow<ResultState<MapOutletBean>> {
        return safeApiCall { apiService.getOutletInfoOnMap() }
    }

    fun getRegionStructure(): Flow<ResultState<MutableList<TreeNode>>> {
        return safeApiCall { apiService.getRegionStructure() }
    }

    fun getCameraUrl(cameraIndexCode: String, protocol: String): Flow<ResultState<Any>> {
        return safeApiCall { apiService.getCameraUrl(cameraIndexCode, protocol) }
    }


}